package com.wisdytech.linkdcs.factory.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.factory.model.Area;
import com.wisdytech.linkdcs.factory.service.TmAreaService;
import com.wisdytech.linkdcs.factory.vo.AreaVO;
import com.wisdytech.linkdcs.system.model.SysUser;

/**
 * 
 * <p>
 * ClassName: AreaAction
 * </p>
 * <p>
 * Description: 区域管理页面的action层
 * </p>
 * <p>
 * Author: chenbin
 * </p>
 * <p>
 * Date: 2018-5-7
 * </p>
 */
@Controller
@RequestMapping("/factory/area")
public class AreaAction {
    /**
     * 注入厂区tmAreaService
     */
    @Autowired
    @Qualifier("tmAreaService")
    private TmAreaService tmAreaService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "factoryModel/area";
    }
    
    /**
     * 分页条件查询区域管理列表信息
     * @param areaCode 区域编码
     * @param areaName 区域名称
     * @param filter 分页参数
     * @return 区域管理列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listAreaByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<AreaVO> listAreaByFilter(String areaCode, String areaName, Filter filter) throws IOException{
    	PageInfo<Area> areas =this.tmAreaService.listAreaByFilter(areaCode, areaName, filter);
        //转换为前台结果集
        List<Area> areaList=areas.getList();
        List<AreaVO> areaVOList=new ArrayList<AreaVO>();
        for(Area area:areaList){
        	areaVOList.add(DataTransformUtil.changeAreaVO(area));
        }
        return new StandardTableResult<AreaVO>(areas.getTotal(),areaVOList);
    }
    
    /**
     * <p>Description: 保存车间对象</p> 
     * @param model 车间对象
     * @return 处理结果对象
     */
    @RequestMapping(value = "/saveArea/save.mvc")
    @ResponseBody
    public StandardResult saveArea(Area area, HttpSession session,HttpServletRequest request) {
       SysUser userSession = BaseUtil.getCurrentUser();
       String loginName = userSession.getUsername();
        if (StringUtils.isBlank(area.getPkId())) {
            tmAreaService.addArea(area, loginName);
        }else {
            tmAreaService.updateArea(area, loginName);
        }
        return StandardResult.getSuccessResult();
    }
    
    /**
     * <p>Description:查询区域信息列表</p> 
     * @return 区域信息列表集合
     * @throws IOException 
     */
    @RequestMapping(value = "/getAreaById/get.mvc")
    @ResponseBody
    public Area getAreaById(String pkId){
        return tmAreaService.getAreaById(pkId);
    }
    
    /**
     * 通过id批量删除厂区信息
     * @param id 主键id
     * @return 删除结果集
     */
    @RequestMapping(value = "/delAreaById/del.mvc")
    @ResponseBody
    public StandardResult delAreaById(String pkId) {
    	tmAreaService.delAreaById(pkId);
        return StandardResult.getSuccessResult();
    }

    /**
     *
     * @param factoryId 厂区id
     * @return StandardResult
     */
    @RequestMapping(value = "/listAllArea/get.mvc")
    @ResponseBody
    public StandardResult listAllArea(String factoryId) {
        List<Area> list = tmAreaService.listAllArea(factoryId);
        return StandardResult.getSuccessResult(list);
    }
}
